package com.tian.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * {@code @description:} 动态线程池配置类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2025-02-17 19:53
 * {@code @version:} 1.0
 */
@Slf4j
@Configuration
public class DynamicThreadPoolConfig {

    @Value("${thread-pool.core-size}")
    private int coreSize;

    @Value("${thread-pool.max-size}")
    private int maxSize;

    @Value("${thread-pool.queue-capacity}")
    private int queueCapacity;

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(coreSize);
        executor.setMaxPoolSize(maxSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("charge-station-dynamicTaskExecutor-");
        executor.initialize();
        executor.setRejectedExecutionHandler((r, executor1) -> log.error("任务" + r + "被拒绝执行"));
        return executor;
    }
}
